package com.jzy.bxb.model.mongo.dao;

import java.util.List;

import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.dao.BasicDAO;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jzy.bxb.core.db.mongo.AbsMongoManager;
import com.jzy.bxb.model.constant.OrderState;
import com.jzy.bxb.model.mongo.entity.Order;

/**
 * 订单
 * 
 * @author JiangZhiYong
 *
 */
@Repository
public class OrderDao extends BasicDAO<Order, Long> {

	private static volatile OrderDao orderDao;

	@Autowired
	public OrderDao(Datastore ds) {
		super(ds);
	}

	public OrderDao(AbsMongoManager manager) {
		super(Order.class, manager.getMongoClient(), manager.getMorphia(), manager.getMongoConfig().getDbName());

	}

	public static OrderDao init(AbsMongoManager mongoManager) {
		if (orderDao == null) {
			synchronized (OrderDao.class) {
				if (orderDao == null) {
					orderDao = new OrderDao(mongoManager);
				}
			}
		}
		return orderDao;
	}

	/**
	 * 保存
	 * 
	 * @param order
	 * @author JiangZhiYong
	 * @date 2017-07-12 QQ:359135103
	 */
	public static void saveOrder(Order order) {
		orderDao.save(order);
	}

	/**
	 * 雇主订单
	 * 
	 * @param employerId
	 * @return
	 * @author JiangZhiYong
	 * @date 2017-07-12 QQ:359135103                                                                        
	 */
	public static List<Order> getEmployerOrders(long employerId, int page, int limit) {
		Query<Order> query = orderDao.createQuery().filter("employerId", employerId).order("state,-createTime").offset(getOffset(page, limit))
				.limit(limit);
		return query.asList();
	}

	public static long getEmployerOrderCount(long employerId) {
		return orderDao.createQuery().filter("employerId", employerId).countAll();
	}

	/**
	 * 帮手所有订单
	 * 
	 * @param employerId
	 * @return
	 * @author JiangZhiYong
	 * @date 2017-07-12 QQ:359135103
	 */
	public static List<Order> getAssistantOrders(long assistantId, int page, int limit) {
		Query<Order> query = orderDao.createQuery().filter("assistantId", assistantId).offset(getOffset(page, limit))
				.limit(limit);
		return query.asList();
	}

	public static long getAssistantOrderCount(long assistantId) {
		return orderDao.createQuery().filter("assistantId", assistantId).countAll();
	}

	/**
	 * 指定状态的帮手
	 * 
	 * @param employerId
	 * @return
	 * @author JiangZhiYong
	 * @date 2017-07-12 QQ:359135103
	 */
	public static List<Order> getAssistantOrders(long assistantId, OrderState orderState, int page, int limit) {
		Query<Order> query = orderDao.createQuery().filter("assistantId", assistantId)
				.filter("state", orderState.ordinal()).order("-createTime").offset(getOffset(page, limit)).limit(limit);
		return query.asList();
	}

	public static long getAssistantOrderCount(long assistantId, OrderState orderState) {
		return orderDao.createQuery().filter("assistantId", assistantId).filter("state", orderState.ordinal())
				.countAll();
	}

	/**
	 * 查询偏移量
	 * 
	 * @param page
	 * @param limit
	 * @return
	 * @author JiangZhiYong
	 * @date 2017-07-12 QQ:359135103
	 */
	private static int getOffset(int page, int limit) {
		if (page < 1) {
			page = 1;
		}
		if (limit < 1) {
			throw new RuntimeException("请求展示数为小于1");
		}
		return (page - 1) * limit;
	}

	public static Order getById(long id) {
		return orderDao.get(id);
	}
}
